package com.stream;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Stream排序 {
	public static void main(String[] args) {
		/**
		 * sorted按照自然顺序排序
		 */
		List<Integer> list1 = Arrays.asList(2,4,3,1);
		list1.stream().sorted().forEach(System.out::println);
		
		/**
		 * 降序排列
		 */
		list1.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println);
		
		//设置排序器，如果排序属性为空，则放在最前
		//Comparator<Student> comoparator = Comparator.comparing(Student::getUserName,Comparator.nullsFirst(Comparator.reverseOrder()));
		/**
		 * 可以依据对象的多个字段进行升序或者降序排列
		 */
		List<Student> list2 = Arrays.asList(new Student("test1",12),new Student("test3",16),new Student("test4",9),new Student("test2",8));
		list2.stream().sorted(Comparator.comparing(Student::getUserName,Comparator.reverseOrder()).thenComparing(Student::getUserAge)).forEach(t -> {System.out.println(t.getUserName() + "-" + t.getUserAge());});;
		

	}
}

class Student{
	private String userName;
	private int userAge;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public int getUserAge() {
		return userAge;
	}
	public void setUserAge(int userAge) {
		this.userAge = userAge;
	}
	public Student(String userName, int userAge) {
		this.userName = userName;
		this.userAge = userAge;
	}
}
